package com.shxc.smarttrade.account.activity;

import android.annotation.SuppressLint;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.account.model.UserTypeModel;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.currency.model.CurrencyModel;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.old.activity.SelectBrokeOpenAccountActivity;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.Loger;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 交易账户 切换
 */
@SuppressLint("NewApi")
@ContentView(R.layout.activity_tradingaccount)
public class TradingAccountActivity extends BaseActivity {

    @ViewInject(R.id.title_textview)
    TextView title_textview;
    @ViewInject(R.id.left_imageview)
    ImageView left_imageview;

    @ViewInject(R.id.tigerwit_view)
    View tigerwit_view;
    @ViewInject(R.id.tigerwit_choose)
    ImageView tigerwit_choose;
    @ViewInject(R.id.tigerwit_account_textview)
    TextView tigerwit_account_textview;
    @ViewInject(R.id.tigerwit_show_view)
    View tigerwit_show_view;

    @ViewInject(R.id.kvb_view)
    View kvb_view;
    @ViewInject(R.id.kvb_choose)
    ImageView kvb_choose;
    @ViewInject(R.id.kvb_account_textview)
    TextView kvb_account_textview;
    @ViewInject(R.id.kvb_show_view)
    View kvb_show_view;

    @ViewInject(R.id.smart_choose)
    ImageView smart_choose;
    @ViewInject(R.id.notice_textview)
    TextView notice_textview;

    @ViewInject(R.id.open_textview)
    TextView open_textview;

    private int RED;
    private int GRAY;

    private int accType = -1;
    private boolean kvbmini = false;
    private boolean tigerwitmini = false;

    private SoundPool soundPool;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title_textview.setText(getString(R.string.tradingaccount_8));
        left_imageview.setVisibility(View.VISIBLE);

        RED = getColor(R.color.account_login_red);
        GRAY = getColor(R.color.message_gray);

        initSound();

        userType();
    }

    private void initSound() {
        soundPool = new SoundPool.Builder().build();
    }

    private void playSound() {
        soundPool.play(
                soundPool.load(this, R.raw.dd, 1),
                1.0f,   //左耳道音量【0~1】
                1.0f,   //右耳道音量【0~1】
                0,     //播放优先级【0表示最低优先级】
                0,     //循环模式【0表示循环一次，-1表示一直循环，其他表示数字+1表示当前数字对应的循环次数】
                1     //播放速度【1是正常，范围从0~2】
        );
    }

    @Event({R.id.left_imageview, R.id.kvb_mini, R.id.kvb_choose, R.id.smart_choose, R.id.open_textview,R.id.tigerwit_mini,R.id.tigerwit_choose})
    private void onClickk(View view) {
        switch (view.getId()) {
            case R.id.left_imageview:
                //关闭
                finish();
                break;
            case R.id.tigerwit_mini:
                //显示/隐藏说明
                tigerwitmini = !tigerwitmini;
                if (tigerwitmini) {
                    tigerwit_show_view.setVisibility(View.VISIBLE);
                } else {
                    tigerwit_show_view.setVisibility(View.GONE);
                }
                break;
            case R.id.tigerwit_choose:
                //切换为tigerwit账户
                accType = 1;
                checkType();
                break;
            case R.id.kvb_mini:
                //显示/隐藏说明
                kvbmini = !kvbmini;
                if (kvbmini) {
                    kvb_show_view.setVisibility(View.VISIBLE);
                } else {
                    kvb_show_view.setVisibility(View.GONE);
                }
                break;
            case R.id.kvb_choose:
                //切换为kvb账户
                accType = 2;
                checkType();
                break;
            case R.id.smart_choose:
                //切换为模拟账户
                accType = 0;
                checkType();
                break;
            case R.id.open_textview:
                JumpTo(SelectBrokeOpenAccountActivity.class);//旧
                break;
        }
    }

    //账户列表
    private void userType() {
        loadingDialog.show();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", spImp.getUserId());

        MyOkHttpUtils.getServiceDatas(map, Contents.ACCOUNT_USERTYPE, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {

                Loger.e("list",response);
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, UserTypeModel.class);
                    if (jam.isSuccess()) {
                        UserTypeModel model = (UserTypeModel) jam.getData();
                        List<UserTypeModel.DataBean.UserAccountBean> list = model.getData().getUserAccount();

                        //是否显示开户按钮
                        if (list.size() == 2) {
                            open_textview.setVisibility(View.GONE);
                        } else {
                            open_textview.setVisibility(View.VISIBLE);
                        }

                        //为UI设值
                        for (int i = 0; i < list.size(); i++) {
                            UserTypeModel.DataBean.UserAccountBean bean = list.get(i);
                            switch (bean.getAccType()) {
                                case 0:
                                    //St模拟账户
                                    if (bean.isNowAccount()) {
                                        smart_choose.setImageResource(R.mipmap.success);
                                    } else {
                                        smart_choose.setImageResource(R.mipmap.unselect_icon);
                                    }
                                    if (bean.isDestroy()) {
                                        notice_textview.setText("该账户已失效");
                                        notice_textview.setTextColor(RED);
                                    } else {
                                        Calendar ca = Calendar.getInstance();
                                        ca.setTimeInMillis(bean.getDesTime());
                                        notice_textview.setText("失效时间：" + ca.get(Calendar.YEAR) + "-" + (ca.get(Calendar.MONTH) + 1 )+ "-" + ca.get(Calendar.DAY_OF_MONTH));
                                        notice_textview.setTextColor(GRAY);
                                    }
                                    break;
                                case 1:
                                    //老虎
                                    tigerwit_view.setVisibility(View.VISIBLE);

                                    if (bean.isNowAccount()) {
                                        tigerwit_choose.setImageResource(R.mipmap.success);
                                    } else {
                                        tigerwit_choose.setImageResource(R.mipmap.unselect_icon);
                                    }
                                    tigerwit_account_textview.setText(getString(R.string.tradingaccount_6) + bean.getMt4Id());
                                    break;
                                case 2:
                                    //KVB
                                    kvb_view.setVisibility(View.VISIBLE);

                                    if (bean.isNowAccount()) {
                                        kvb_choose.setImageResource(R.mipmap.success);
                                    } else {
                                        kvb_choose.setImageResource(R.mipmap.unselect_icon);
                                    }
                                    kvb_account_textview.setText(getString(R.string.tradingaccount_6) + bean.getMt4Id());
                                    break;
                            }
                        }
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }

    //账户切换
    private void checkType() {
        loadingDialog.show();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", spImp.getUserId());
        map.put("accType", accType);

        MyOkHttpUtils.getServiceDatas(map, Contents.ACCOUNT_CHECKTYPE, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, CurrencyModel.class);
                    if (jam.isSuccess()) {
                        spImp.setAccType(accType);
                        Loger.e("acctype",spImp.getAccType());
                        switch (accType) {
                            case 0:
                                smart_choose.setImageResource(R.mipmap.success);
                                tigerwit_choose.setImageResource(R.mipmap.unselect_icon);
                                kvb_choose.setImageResource(R.mipmap.unselect_icon);
                                break;
                            case 1:
                                smart_choose.setImageResource(R.mipmap.unselect_icon);
                                tigerwit_choose.setImageResource(R.mipmap.success);
                                kvb_choose.setImageResource(R.mipmap.unselect_icon);
                                break;
                            case 2:
                                smart_choose.setImageResource(R.mipmap.unselect_icon);
                                tigerwit_choose.setImageResource(R.mipmap.unselect_icon);
                                kvb_choose.setImageResource(R.mipmap.success);
                                break;
                        }
                        playSound();
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }
}
